package com.happyclub.lbj.service.impl;

import com.happyclub.lbj.service.IProduct;
import com.happyclub.lbj.service.IProductColor;
import com.happyclub.lbj.service.IProductSize;
import com.happyclub.lbjdao.*;
import com.happyclub.paul.dao.PhotoLibraryRepostitory;
import com.happyclub.paul.service.IFastdfsService;
import com.happyclub.paul.service.IFilePathService;
import com.happyclub.paul.service.IPhotoLibraryService;
import com.happyclub.pojo.TbPhotoLibrary;
import com.happyclub.pojo.TbProduct;
import com.happyclub.pojo.TbProductColor;
import com.happyclub.pojo.TbProductSize;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.happyclub.errormsg.ErrorMsgProvider.SYSTEM_BUSY;

/**
 * @author 高欢
 * @description TODO
 * @date 2019/4/10
 */
@Service
public class ProductImpl implements IProduct {
    @Autowired
    Product iProduct;
    @Autowired
    IFilePathService iFilePathService;//paul
    @Autowired
    IFilePathService filePathService;//paul
    @Autowired
    IFastdfsService fastdfsService;//paul
    @Autowired
    PhotoLibraryRepostitory photoLibraryRepostitory;//paul
    @Autowired
    TbProductColorMap iProductColorMap;
    @Autowired
    TbProductSizeMap iProductSizeMap;
    @Autowired
    IPhotoLibraryService iPhotoLibraryService;//paul
    @Autowired
    ProductColor iProductColor;
    @Autowired
    ProductSize iProductSize;

    @Transactional
    @Override
    public JsonResult saveProduct(TbProduct tbProduct, MultipartFile[] multipartFiles) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        JsonResult jsonResult1 = null;
        TbProduct save = iProduct.save(tbProduct);
        if (save==null){
            jsonResult.setErrorCode("500");
            jsonResult.setMsg(SYSTEM_BUSY);
            return jsonResult;
        }
        String proId = save.getId();
        if (multipartFiles.length>0){
            jsonResult1 = uploadProPhotos(multipartFiles);
        }
        iProduct.updateImgUrl(jsonResult1.getResult().toString(), proId);
        TbProduct tbProduct1 = iProduct.findById(proId).get();
        jsonResult.setResult(tbProduct1);
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult saveProductColorAndSize(String productId, String color, String size) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        if (color!=null&&size!=null){
            String[] colorId = color.split(",");
            String[] sizeId = size.split(",");
            for (String c:colorId){
                com.happyclub.pojo.TbProductColorMap tbProductColorMap = new com.happyclub.pojo.TbProductColorMap();
                tbProductColorMap.setProductId(productId);
                tbProductColorMap.setProductColor(c);
                iProductColorMap.save(tbProductColorMap);
            }
            for (String s:sizeId){
                com.happyclub.pojo.TbProductSizeMap tbProductSizeMap = new com.happyclub.pojo.TbProductSizeMap();
                tbProductSizeMap.setProductId(productId);
                tbProductSizeMap.setSizeId(s);
                iProductSizeMap.save(tbProductSizeMap);
            }
            return jsonResult;
        }
        jsonResult.setMsg(SYSTEM_BUSY);
        jsonResult.setErrorCode("500");
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult updateProUpper(String productId, Integer upper) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        iProduct.updateProUpper( upper,productId);
        return jsonResult;
    }

    @Override
    public JsonResult findAllProByUpper() {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbProduct> tbProductsByUpper = iProduct.findTbProductsByUpper(1);
        if (tbProductsByUpper!=null){
            for (TbProduct p:tbProductsByUpper){
                String finaUrl = "";
                String imgUrl = p.getImgUrl();
                String[] http = imgUrl.split(",");
                for (String h : http){
                    TbPhotoLibrary photo = iPhotoLibraryService.queryPhotoLibraryByPhotoId(h);
                    String realUrl = photo.getPhotoUrl();
                    finaUrl = finaUrl + realUrl + ",";
                }
                p.setImgUrl(finaUrl);
            }
            jsonResult.setResult(tbProductsByUpper);
            return jsonResult;
        }
        jsonResult.setMsg("没有可以上架的商品!");
        return jsonResult;
    }

    @Override
    public JsonResult findProDetail(String productId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<com.happyclub.pojo.TbProductColor> tbProductColors = new ArrayList<>();
        List<com.happyclub.pojo.TbProductSize> tbProductSizes = new ArrayList<>();
        TbProduct tbProduct = iProduct.findById(productId).get();
        List<com.happyclub.pojo.TbProductColorMap> colorMaps = iProductColorMap.findTbProductColorMapsByProductId(productId);
        List<com.happyclub.pojo.TbProductSizeMap> sizeMaps = iProductSizeMap.findTbProductSizeMapsByProductId(productId);
        for (com.happyclub.pojo.TbProductColorMap pc:colorMaps){
            TbProductColor tbProductColor = iProductColor.findById(pc.getProductColor()).get();
            tbProductColors.add(tbProductColor);
        }
        for (com.happyclub.pojo.TbProductSizeMap ps:sizeMaps){
            com.happyclub.pojo.TbProductSize tbProductSize = iProductSize.findTbProductSizeById(ps.getSizeId());
            tbProductSizes.add(tbProductSize);
        }
        tbProduct.setColorList(tbProductColors);
        tbProduct.setSizeList(tbProductSizes);
        jsonResult.setResult(tbProduct);
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult addLookNum(String id) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        iProduct.updateProLookNum(id);
        return jsonResult;
    }

    //商品图片上传,返回相片ID
    public JsonResult uploadProPhotos(MultipartFile[] multipartFiles) {
        String imgUrl="";
        JsonResult jsonResult =null;
        for(MultipartFile multipartFile:multipartFiles) {
            jsonResult = filePathService.upload(multipartFile);
            String errorCode = jsonResult.getErrorCode();
            System.out.println("测试文件是否上传到本地：" + errorCode);
            if ("error".equals(errorCode)) {             //上传失败
                return jsonResult;
            }
            if ("OK".equals(errorCode)) {               //上传成功，提交到fastdfs,返回id
                String filePath = (String) jsonResult.getResult();
                try {
                    String fastdfsId = fastdfsService.uploadFile(filePath);     //获取到标识
                    String photoUrl = "http://47.107.81.116/" + fastdfsId;
                    //将上传的相片保存到相片表，为 未审核状态 0 将相片ID返回,存到商品表中逗号隔开
                    TbPhotoLibrary tbPhotoLibrary = new TbPhotoLibrary();
                    tbPhotoLibrary.setPhotoUrl(photoUrl);
                    tbPhotoLibrary.setOk(0L);
                    TbPhotoLibrary save = photoLibraryRepostitory.save(tbPhotoLibrary);
                    if (save==null){
                        jsonResult.setErrorCode("500");
                        jsonResult.setMsg(SYSTEM_BUSY);
                        return jsonResult;
                    }
                    imgUrl=imgUrl+save.getId()+",";
                } catch (Exception e) {
                    jsonResult.setMsg("出现异常");
                    jsonResult.setErrorCode("500");
                    e.printStackTrace();
                }
            }
        }
        jsonResult.setResult(imgUrl);
        return  jsonResult;
    }


}
